package com.learn.english.controller;

import com.learn.english.domain.R;
import com.learn.english.model.dto.CourseChapterDTO;
import com.learn.english.model.entity.CourseChapter;
import com.learn.english.service.ICourseChapterService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程章节管理控制器
 */
@RestController
@RequestMapping("/course/chapter")
@RequiredArgsConstructor
public class CourseChapterController {

    private final ICourseChapterService courseChapterService;

    /**
     * 根据课程ID获取章节列表
     * @param courseId 课程ID
     * @return 章节列表
     */
    @GetMapping("/list")
    public R<List<CourseChapter>> getChapterList(@RequestParam Long courseId) {
        List<CourseChapter> chapterList = courseChapterService.getChaptersByCourseId(courseId);
        return R.ok(chapterList);
    }

    /**
     * 获取章节详情
     * @param id 章节ID
     * @return 章节详情
     */
    @GetMapping("/detail/{id}")
    public R<CourseChapter> getChapterDetail(@PathVariable Long id) {
        CourseChapter chapter = courseChapterService.getById(id);
        return R.ok(chapter);
    }

    /**
     * 添加章节
     * @param chapterDTO 章节信息
     * @return 添加结果
     */
    @PostMapping("/add")
    public R<Boolean> addChapter(@RequestBody CourseChapterDTO chapterDTO) {
        boolean result = courseChapterService.saveChapter(chapterDTO);
        return result ? R.ok(true) : R.fail();
    }

    /**
     * 更新章节
     * @param chapterDTO 章节信息
     * @return 更新结果
     */
    @PutMapping("/update")
    public R<Boolean> updateChapter(@RequestBody CourseChapterDTO chapterDTO) {
        boolean result = courseChapterService.updateChapter(chapterDTO);
        return result ? R.ok(true) : R.fail();
    }

    /**
     * 删除章节
     * @param id 章节ID
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    public R<Boolean> deleteChapter(@PathVariable Long id) {
        boolean result = courseChapterService.removeById(id);
        return result ? R.ok(true) : R.fail();
    }

    /**
     * 批量删除章节
     * @param ids 章节ID列表
     * @return 删除结果
     */
    @DeleteMapping("/batchDelete")
    public R<Boolean> batchDeleteChapters(@RequestBody List<Long> ids) {
        boolean result = courseChapterService.removeBatchByIds(ids);
        return result ? R.ok(true) : R.fail();
    }
}
